package com.tencent.qcloud.roomservice.webrtc.utils;

import com.tencent.qcloud.roomservice.webrtc.pojo.LivePlay;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

public class LiveUtils {

    /**
     * 获取推流地址
     * 如果不传key和过期时间，将返回不含防盗链的url
     * @param bizId 您在腾讯云分配到的bizid
     *        streamId 您用来区别不同推流地址的唯一id
     *        key 安全密钥
     *        time 过期时间 sample 2016-11-12 12:00:00
     * function getPushUrl($bizId, $streamId, $key = null, $time = null)
     * @return String url */
    private static String pubkey="9fffdcb2b060e424b55703e2b4194771";
    private static String bizId="25849";
    //rtmp://25849.livepush.myqcloud.com/live/25849_d7b1d2bacc?bizid=25849&txSecret=032cc8bf5041c455c71612dfb4bd1bef&txTime=5B213F7F
    public static LivePlay genLivePlay(String uid) {
        Long OverTime = new Date().getTime() + 24*60*60*1000;
        String streamId = bizId + "_did" + uid;
        String pushUrl = String.format("rtmp://%s.livepush.myqcloud.com/live/%s?bizid=%s&%s",
                bizId, streamId, bizId, getSafeUrl(pubkey, streamId, OverTime));
        String rtmp = String.format("rtmp://%s.liveplay.myqcloud.com/live/%s", bizId, streamId);
        String flv = String.format("http://%s.liveplay.myqcloud.com/live/%s.flv", bizId, streamId);
        String hls = String.format("http://%s.liveplay.myqcloud.com/live/%s.m3u8", bizId, streamId);
        return new LivePlay(pushUrl, flv, rtmp, hls);
    }
    public static void main(String[] args) {
        LivePlay p = genLivePlay("10011");
        System.out.println(p.getPushUrl());
        System.out.println(p.getRtmpPlay());
        System.out.println(p.getFlvPlay());
        System.out.println(p.getHlsPlay());
    }

    private static final char[] DIGITS_LOWER =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    /*
     * KEY+ stream_id + txTime
     */
    private static String getSafeUrl(String key, String streamId, long txTime) {
        String input = new StringBuilder().
                append(key).
                append(streamId).
                append(Long.toHexString(txTime).toUpperCase()).toString();

        String txSecret = null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            txSecret  = byteArrayToHexString(
                    messageDigest.digest(input.getBytes("UTF-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return txSecret == null ? "" :
                new StringBuilder().
                        append("txSecret=").
                        append(txSecret).
                        append("&").
                        append("txTime=").
                        append(Long.toHexString(txTime).toUpperCase()).
                        toString();
    }

    private static String byteArrayToHexString(byte[] data) {
        char[] out = new char[data.length << 1];

        for (int i = 0, j = 0; i < data.length; i++) {
            out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS_LOWER[0x0F & data[i]];
        }
        return new String(out);
    }
}
